package com.letv.android.client.episode.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.letv.android.young.client.R;
import com.letv.android.client.episode.callback.PinnedHeaderExpListViewCallBack;
import com.letv.android.client.episode.callback.WatchFocusSeekToCallBack;
import com.letv.android.client.episode.utils.EpisodeUtils;
import com.letv.android.client.widget.LetvImageView;
import com.letv.core.bean.AlbumNew;
import com.letv.core.bean.AlbumNewListByDateBean;
import com.letv.core.bean.VideoBean;
import com.letv.core.download.image.ImageDownloader;
import com.letv.core.utils.LogInfo;
import com.letv.core.utils.StringUtils;
import com.letv.core.utils.UIsUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
//import com.letv.download.bean.DownloadVideo;
//import com.letv.download.manager.DownloadManager;

public class DetailHalfPlayExpandableListByDateAdapterPlayerLibs extends BaseExpandableListAdapter implements PinnedHeaderExpListViewCallBack {
    private Context mContext;
    private ViewHolderGroup viewHolderGroup;
    private LayoutInflater layoutInflater = null;
    private ExpandableListView listView;
    private VideoBean mVideoPlayerLibs;
    private long aid;
    private long curId;
    private AlbumNewListByDateBean albumNewListByDatePlayerLibs;
    private WatchFocusSeekToCallBack mWatchFocusSeekToCallBack;
    private int mPinnedHeaderBackgroundColor;
    private int mPinnedHeaderTextColor;
    private int oldSelectedIndexView = 0;
    private int groupCount = 0;
    private LinkedList<String> groupTitles = new LinkedList<String>();
    private ArrayList<ArrayList<VideoBean>> groupItems;
    
    private int loadGroupPosition;			//正在加载组位置
    private boolean loadProgressingFlag;	//当前正在加载组是否处于正在加载状态

    private ViewGroup watchFocusView;

    /**
     * 是否是下载界面
     */
    private boolean isDownLoad;

    /**
     * 全屏or竖屏
     */
    private boolean isLandscape = false;

    public void setIsDownLoad(boolean isDownLoad) {
        this.isDownLoad = isDownLoad;
    }
    
    public final class ViewHolderGroup {
        public TextView titleView;
        public ImageView arrowView;
        public ProgressBar progressBarView;
    }

    public DetailHalfPlayExpandableListByDateAdapterPlayerLibs(Context context) {
        mContext = context;
        layoutInflater = LayoutInflater.from(context);
        mPinnedHeaderBackgroundColor = mContext.getResources().getColor(R.color.letv_color_ffdfdfdf);
        mPinnedHeaderTextColor = mContext.getResources().getColor(R.color.letv_color_ff969696);
        groupItems = new ArrayList<ArrayList<VideoBean>>();
    }

    public void setIsLandscape(boolean isLandscape) {
        this.isLandscape = isLandscape;
    }

    public void setListView(ExpandableListView listView) {
        this.listView = listView;
    }

    public void setList(AlbumNewListByDateBean albumNewListByDatePlayerLibs,int groupPosition) {
        this.albumNewListByDatePlayerLibs = albumNewListByDatePlayerLibs;
        addDataToGroup(groupPosition);
        listView.expandGroup(groupPosition);
        parseData();
    }

    public void notifyWatchFocusView(VideoBean videoPlayerLibs,WatchFocusSeekToCallBack watchFocusSeekToCallBack){
        oldSelectedIndexView = 0;
        this.mVideoPlayerLibs = videoPlayerLibs;
        this.mWatchFocusSeekToCallBack = watchFocusSeekToCallBack;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        if(groupItems != null && groupItems.get(groupPosition) != null 
        		&& groupItems.get(groupPosition).size() > 0 
        		&& childPosition < groupItems.get(groupPosition).size()){
            return groupItems.get(groupPosition).get(childPosition);
        }
        return null;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }
    
    public void setProgressingParam(int groupPosition, boolean progressingFlag) {
    	loadGroupPosition = groupPosition;
    	loadProgressingFlag = progressingFlag;
    }


    /**
     * 创建竖屏列表
     */
    private void createPortraitVideosList(final ViewChildPortraitHoler mViewHolder, VideoBean mVideo, View convertView, int groupPosition, int childPosition){
        if (mVideo == null) {
            return;
        }
        switch (mVideo.getCid()){
            case AlbumNew.Channel.TYPE_H265:
            case AlbumNew.Channel.TYPE_MOVIE:
            case AlbumNew.Channel.TYPE_DOLBY:
                mViewHolder.titleView.setVisibility(View.VISIBLE);
                mViewHolder.titleView.setText(mVideo.getNameCn());
                if(!TextUtils.isEmpty(mVideo.getSubTitle())){
                    mViewHolder.subTitleView.setVisibility(View.VISIBLE);
                    mViewHolder.subTitleView.setText(mVideo.getSubTitle());
                    mViewHolder.subTitleView.setMaxLines(1);
                    mViewHolder.titleView.setMaxLines(1);
                } else {
                    mViewHolder.subTitleView.setVisibility(View.GONE);
                    mViewHolder.titleView.setMaxLines(2);
                }
                if(!TextUtils.isEmpty(mVideo.getDuration()+"")) {
                    mViewHolder.videoTime.setVisibility(View.VISIBLE);
                    mViewHolder.videoTime.setText(EpisodeUtils.stringForTimeNoHour(mVideo.getDuration() * 1000));
                } else {
                    mViewHolder.videoTime.setVisibility(View.GONE);
                }
                break;
            case AlbumNew.Channel.TYPE_TVSHOW:
                mViewHolder.titleView.setVisibility(View.VISIBLE);
                if(!TextUtils.isEmpty(mVideo.getNameCn())) {
                	mViewHolder.titleView.setText(mVideo.getNameCn());
                } else {
                	mViewHolder.titleView.setText(mVideo.getSubTitle());                    
                }
                if(!TextUtils.isEmpty(mVideo.getGuest())){
            		String[] guests = mVideo.getGuest().trim().split(" ");
                	String guestStr = "";
                	for (int i = 0; i < guests.length; i++) {
                		if (!"".equals(guests[i])) {
            				if (i==guests.length-1) {
            					guestStr+=guests[i];
            				}else{
            					guestStr+=guests[i]+",";
            				}
            			}
            		}
                    mViewHolder.subTitleView.setVisibility(View.VISIBLE);
                    mViewHolder.subTitleView.setText(mContext.getResources().getString(R.string.play_half_video_guest_title) + guestStr);
                    mViewHolder.subTitleView.setMaxLines(1);
                    mViewHolder.titleView.setMaxLines(2);
                } else {
                    mViewHolder.subTitleView.setVisibility(View.GONE);
                    mViewHolder.titleView.setMaxLines(2);
                }
                //综艺显示需要格式化时间
                mViewHolder.videoTime.setVisibility(View.VISIBLE);
                if (!TextUtils.isEmpty(mVideo.getReleaseDate())) {
                	mViewHolder.videoTime.setText(mVideo.getReleaseDate());
                }
                else if (!TextUtils.isEmpty(mVideo.getEpisode())) {
                	mViewHolder.videoTime.setText(EpisodeUtils.formatData(mVideo.getEpisode()));
                }
                break;
            case AlbumNew.Channel.TYPE_MUSIC:
                mViewHolder.titleView.setVisibility(View.VISIBLE);
                mViewHolder.titleView.setText(mVideo.getNameCn());
                if(!TextUtils.isEmpty(mVideo.getSinger())){
                    mViewHolder.subTitleView.setVisibility(View.VISIBLE);
                    mViewHolder.subTitleView.setText("歌手:"+mVideo.getSinger());
                    mViewHolder.subTitleView.setMaxLines(1);
                    mViewHolder.titleView.setMaxLines(1);
                } else {
                    mViewHolder.subTitleView.setVisibility(View.GONE);
                    mViewHolder.titleView.setMaxLines(2);
                }
                mViewHolder.videoTime.setVisibility(View.VISIBLE);
                mViewHolder.videoTime.setText(EpisodeUtils.stringForTimeNoHour(mVideo.getDuration()* 1000));
                break;
            case AlbumNew.Channel.TYPE_DOCUMENT_FILM:
                mViewHolder.titleView.setVisibility(View.VISIBLE);
                mViewHolder.titleView.setMaxLines(2);
                if(!TextUtils.isEmpty(mVideo.getNameCn())) {
                    mViewHolder.titleView.setText(mVideo.getNameCn());
                } else {
                    mViewHolder.titleView.setText(mVideo.getSubTitle());
                }
                mViewHolder.videoTime.setVisibility(View.VISIBLE);
                if (!TextUtils.isEmpty(mVideo.getReleaseDate())) {
                	mViewHolder.videoTime.setText(mVideo.getReleaseDate());
                }
                else if (!TextUtils.isEmpty(mVideo.getEpisode())) {
                	mViewHolder.videoTime.setText(EpisodeUtils.formatData(mVideo.getEpisode()));
                }
                break;
            //体育/热点/NBA/财经/汽车/旅游
            //缺少财经---------
            case AlbumNew.Channel.TYPE_PE:
            case AlbumNew.Channel.TYPE_NBA:
            case AlbumNew.Channel.TYPE_FINANCIAL:
            case AlbumNew.Channel.TYPE_CAR:
            case AlbumNew.Channel.TYPE_TOURISM:
            case AlbumNew.Channel.TYPE_FASHION:
                mViewHolder.titleView.setVisibility(View.VISIBLE);
                mViewHolder.titleView.setMaxLines(2);
                if(!TextUtils.isEmpty(mVideo.getNameCn())) {
                    mViewHolder.titleView.setText(mVideo.getNameCn());
                } else {
                    mViewHolder.titleView.setText(mVideo.getSubTitle());
                }
                mViewHolder.videoTime.setVisibility(View.VISIBLE);
                if (!TextUtils.isEmpty(mVideo.getReleaseDate())) {
                	mViewHolder.videoTime.setText(mVideo.getReleaseDate());
                }
                else if (!TextUtils.isEmpty(mVideo.getEpisode())) {
                	mViewHolder.videoTime.setText(EpisodeUtils.formatData(mVideo.getEpisode()));
                }
                else {
                	mViewHolder.videoTime.setText(EpisodeUtils.stringForTimeNoHour(mVideo.getDuration()* 1000));
                }
                break;
            case AlbumNew.Channel.TYPE_JOY:
                mViewHolder.titleView.setVisibility(View.VISIBLE);
                mViewHolder.titleView.setMaxLines(2);
                if(!TextUtils.isEmpty(mVideo.getNameCn())) {
                    mViewHolder.titleView.setText(mVideo.getNameCn());
                } else {
                    mViewHolder.titleView.setText(mVideo.getSubTitle());
                }
                mViewHolder.videoTime.setVisibility(View.VISIBLE);
                mViewHolder.videoTime.setText(EpisodeUtils.stringForTimeNoHour(mVideo.getDuration()* 1000));
                break;
            default:
                mViewHolder.titleView.setVisibility(View.VISIBLE);
                mViewHolder.titleView.setMaxLines(2);
                if(!TextUtils.isEmpty(mVideo.getNameCn())) {
                    mViewHolder.titleView.setText(mVideo.getNameCn());
                } else {
                    mViewHolder.titleView.setText(mVideo.getSubTitle());
                }
                mViewHolder.videoTime.setVisibility(View.VISIBLE);
                mViewHolder.videoTime.setText(EpisodeUtils.stringForTimeNoHour(mVideo.getDuration()* 1000));
                break;
        }
        LogInfo.log("clf", "..STATE_FIRST_PLAYING..2");

        if(mVideoPlayerLibs !=null && mVideoPlayerLibs.getId() == mVideo.getId() && !isLandscape){
            ArrayList<VideoBean.WatchingFocusItem> watchingList = mVideoPlayerLibs.getWatchingFocusList();
            if(watchingList !=null && watchingList.size() > 0){
            	LogInfo.log("clf", "..STATE_FIRST_PLAYING..3");
                mViewHolder.watchFocusView.setVisibility(View.VISIBLE);
                WatchStatisticalData(mViewHolder);
                addWatchFocusView(mViewHolder.watchFocusView, watchingList);
            }else{
                mViewHolder.watchFocusView.setVisibility(View.GONE);
                mViewHolder.watchFocusView.removeAllViews();
            }
        }else{
            mViewHolder.watchFocusView.removeAllViews();
            mViewHolder.watchFocusView.setVisibility(View.GONE);
        }

        if (mVideo.getPlayCount() > 0) {
        	mViewHolder.playNumView.setVisibility(View.VISIBLE);
        	mViewHolder.playNumView.setText(EpisodeUtils.getPlayCountsToStr(mVideo.getPlayCount()));
        }
        else {
        	mViewHolder.playNumView.setVisibility(View.GONE);
        }
        LogInfo.log("carey", "getview  " + mVideo.getPlayCount());

        String strImageUrl = getImageUrl(mVideo);
        if (!TextUtils.isEmpty(strImageUrl)) {
        	mViewHolder.videoImageView.setImageResource(R.drawable.letv_poster_defualt_pic);
        	ImageDownloader.getInstance(mContext).download(mViewHolder.videoImageView, strImageUrl);
        }
        else {
        	mViewHolder.videoImageView.setImageResource(R.drawable.letv_poster_defualt_pic);
        }

        //下载状态显示
//        DownloadVideo downloadVideo = DownloadManager.getDownloadFinishVideo(mVideo.getVid());
//        if (downloadVideo != null) {
//            mViewHolder.localTextView.setVisibility(View.VISIBLE);
//            mViewHolder.localTextView.setTextColor(mContext.getResources().getColor(R.color.letv_color_ff67b716));
//            mViewHolder.localTextView.setText(R.string.download_tip);
//        } else {
//            mViewHolder.localTextView.setVisibility(View.GONE);
//        }

        if (curId == mVideo.getId()) {
            mViewHolder.titleView.setTextColor(mContext.getResources().getColor(R.color.letv_color_ffffffff));
            mViewHolder.subTitleView.setTextColor(mContext.getResources().getColor(R.color.letv_color_ffffffff));
            mViewHolder.playNumView.setTextColor(mContext.getResources().getColor(R.color.letv_color_ffffffff));
            mViewHolder.playNumView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.letv_playnum_select, 0,0,0);
            mViewHolder.localTextView.setTextColor(mContext.getResources().getColor(R.color.letv_color_ffffffff));
            convertView.setBackgroundResource(R.color.letv_color_ff00a0e9);
        } else {
            mViewHolder.titleView.setTextColor(mContext.getResources().getColor(R.color.letv_color_ff5c5c5c));
            mViewHolder.subTitleView.setTextColor(mContext.getResources().getColor(R.color.letv_color_ff969696));
            mViewHolder.playNumView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.letv_playnum_normal, 0, 0, 0);
            mViewHolder.playNumView.setTextColor(mContext.getResources().getColor(R.color.letv_color_ff969696));
//            mViewHolder.localTextView.setTextColor(mContext.getResources().getColor(R.color.letv_color_ff67b716));
//            convertView.setBackgroundResource(R.drawable.letv_half_play_relate_item_selecter);
        }
    }

    /**
     * 创建全屏的列表
     */
    private void createLandscapeVideosList(ViewChildLandscapeHoler mViewHolder, final VideoBean mVideo, View convertView){
        mViewHolder.video_title_singer.setVisibility(View.GONE);
        switch (mVideo.getCid()) {
            case AlbumNew.Channel.TYPE_H265:
            case AlbumNew.Channel.TYPE_MOVIE:
            case AlbumNew.Channel.TYPE_DOLBY:
                mViewHolder.titleView.setText(mVideo.getNameCn());
                break;
            case AlbumNew.Channel.TYPE_MUSIC:
                mViewHolder.titleView.setText(mVideo.getNameCn());
                if (mVideo.getSinger()!=null) {
                    mViewHolder.video_title_singer.setVisibility(View.VISIBLE);
                    mViewHolder.video_title_singer.setText(mVideo.getSinger());
                }
                break;
            case AlbumNew.Channel.TYPE_TVSHOW:
                mViewHolder.titleView.setText(mVideo.getNameCn());
                break;
            default:
                mViewHolder.titleView.setText(mVideo.getNameCn());
                break;
        }
//        DownloadVideo dbBean = null;
//        if (mVideo.canDownload()) {
//            dbBean = DownloadManager.getDownloadedVideoData(String.valueOf(mVideo.getId()));
//        }
//        if(isDownLoad){
//            mViewHolder.linearlayout_root.setSelected(false);
//            recordVideoStatus(isDownLoad, dbBean, mVideo, convertView, mViewHolder);
//        }else{
//            recordVideoStatus(isDownLoad, dbBean, mVideo, convertView, mViewHolder);
            if(curId == mVideo.getId()){
                mViewHolder.linearlayout_root.setSelected(true);
            }else{
                mViewHolder.linearlayout_root.setSelected(false);
            }
//        }
    }

//    private void recordVideoStatus(boolean isDownLoad,DownloadVideo dbBean,VideoBean mVideo,View convertView,ViewChildLandscapeHoler mViewHolder){
//        if (dbBean !=null) {
//            if (dbBean.state == 4) {// 完成
//                mViewHolder.checkView.setVisibility(View.GONE);
//                mViewHolder.localView.setVisibility(View.VISIBLE);
//                mViewHolder.localView.setText(R.string.download_tip);
//            }else{
//                if(isDownLoad){
//                    mViewHolder.checkView.setVisibility(View.VISIBLE);
//                }else{
//                    mViewHolder.checkView.setVisibility(View.GONE);
//                }
//                mViewHolder.localView.setVisibility(View.GONE);
//            }
//        }else{
//            mViewHolder.checkView.setVisibility(View.GONE);
//            mViewHolder.localView.setVisibility(View.GONE);
//        }
//    }
    /**
     * add 5.8.1 看点的曝光
     * @param mViewHolder
     */
	private void WatchStatisticalData(final ViewChildPortraitHoler mViewHolder) {
		try {
			if (mViewHolder.watchFocusView.getVisibility() == View.VISIBLE) {
				// Commented by chenhong for debug reason
//                StatisticsUtils.staticticsInfoPost(mContext, "19", "h25", null, -1, null, "031",mVideoPlayerLibs.getCid() + "", mVideoPlayerLibs.getPid()
//								+ "", mVideoPlayerLibs.getId() + "", "-", "-");
//				LogInfo.log("glh", "DetailHalfPlayExpandableListByDateAdapterPlayerLibs  show");
            }
        } catch (Exception e) {
			e.printStackTrace();
		}
	}
    
    public int getWatchFocusListCount() {
    	if (mVideoPlayerLibs != null && mVideoPlayerLibs.getWatchingFocusList() != null) {
    		return mVideoPlayerLibs.getWatchingFocusList().size();
    	}
    	else {
    		return 0;
    	}
    }

    /**
     * 动态添加看点view
     * */
    public void addWatchFocusView(final LinearLayout view,ArrayList<VideoBean.WatchingFocusItem> watchingList){
    	if(view.getChildCount() !=0){
            return;
        }
        watchFocusView = view;
        
        boolean isPlayInit = false;
        if(mWatchFocusSeekToCallBack != null && mWatchFocusSeekToCallBack.hasInited()){
        	isPlayInit = mWatchFocusSeekToCallBack.hasInited();
        }
        view.removeAllViews();
        VideoBean.WatchingFocusItem item = null;
        int listSize = watchingList.size();
        for(int i =0 ;i<listSize;i++){
            final TextView watchingTextView = new TextView(mContext);
//            LinearLayout.LayoutParams viewLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, UIsPlayerLibs.dipToPx(40));
            watchingTextView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, UIsUtils.dipToPx(mContext,40)));
            
            if(i == 0){
            	watchingTextView.setPadding(24, 0, 20, 0);
//            	viewLayoutParams.setMargins(24, 0, 20, 0);
                Drawable leftDrawable = mContext.getResources().getDrawable(R.drawable.letv_watch_focus_end);
                leftDrawable.setBounds(0, 0, leftDrawable.getMinimumWidth(), leftDrawable.getMinimumHeight());//必须设置图片大小，否则不显示
                watchingTextView.setCompoundDrawablePadding(10);
                watchingTextView.setCompoundDrawables(leftDrawable, null, null, null);
                if (TextUtils.isEmpty(mVideoPlayerLibs.getSubTitle()) || mVideoPlayerLibs.getSubTitle().equals("null"))
                {
                	watchingTextView.setVisibility(View.GONE);
                }
                else {
                	watchingTextView.setText("完整版: " + mVideoPlayerLibs.getSubTitle());
                }
                watchingTextView.setTextColor(mContext.getResources().getColor(R.color.letv_color_ff00a0e9));
            }else{
                item = watchingList.get(i-1);
//                viewLayoutParams.setMargins(64, 0, 20, 0);
                watchingTextView.setPadding(64, 0, 20, 0);
                watchingTextView.setText(EpisodeUtils.string2FormatStringTime(item.getTimeDot()) + "  "+ item.getDesc());
                if(isPlayInit){
                	watchingTextView.setTextColor(mContext.getResources().getColor(R.color.letv_color_ff5d5d5d));
                }else{
                	watchingTextView.setTextColor(mContext.getResources().getColor(R.color.letv_color_ff5d5d5d));
                }
            }
            watchingTextView.setGravity(Gravity.CENTER_VERTICAL);
            watchingTextView.setTextSize(13);
            watchingTextView.setSingleLine();
            watchingTextView.setEllipsize(TextUtils.TruncateAt.END);
            watchingTextView.setTag(2*i);
            view.addView(watchingTextView);	//, viewLayoutParams
            final String time = item == null ? "00:00:00":item.getTimeDot();
            watchingTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mWatchFocusSeekToCallBack != null && mWatchFocusSeekToCallBack.hasInited()){
                    	mWatchFocusSeekToCallBack.seekTo(StringUtils.stringToMillisecond(time));
                    }else{
                    	return;
                    }
                    TextView oldSelcetedview= (TextView)view.getChildAt(oldSelectedIndexView);
                    oldSelcetedview.setCompoundDrawables(null,null,null,null);
                    oldSelcetedview.setPadding(64, 0, 20, 0);
                    oldSelcetedview.setTextColor(mContext.getResources().getColor(R.color.letv_color_ff5d5d5d));
                    Drawable leftDrawable = mContext.getResources().getDrawable(R.drawable.letv_watch_focus_end);
                    leftDrawable.setBounds(0, 0, leftDrawable.getMinimumWidth(), leftDrawable.getMinimumHeight());//必须设置图片大小，否则不显示
                    ((TextView)v).setCompoundDrawablePadding(10);
                    ((TextView)v).setCompoundDrawables(leftDrawable,null,null,null);
                    ((TextView) v).setTextColor(mContext.getResources().getColor(R.color.letv_color_ff00a0e9));
                    v.setPadding(24, 0, 20, 0);
                    oldSelectedIndexView = (Integer)v.getTag();
                    
                    //5.7统计需求
                    try {
                    	if (watchingTextView.getText().toString().contains("完整版")) {
                            LogInfo.log("glh", "点播-期数-完整版 ,name="+mVideoPlayerLibs.getSubTitle());
                         // Commented by chenhong for debug reason
//                            EpisodeUtils.staticticsInfoPost(mContext, "0", "h2122", null, -1, -1, null, PageIdConstant.halpPlayPage, mVideoPlayerLibs.getCid()+"", mVideoPlayerLibs.getPid()+"", mVideoPlayerLibs.getId()+"", null, null);
                        }
					} catch (Exception e) {
						e.printStackTrace();
					}
                }
            });
            
            if (i != listSize -1) {
            	TextView line2view = new TextView(mContext);
	            line2view.setBackgroundResource(R.color.letv_color_ffdfdfdf);
	            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,1); 
	            layoutParams.setMargins(64, 0, 0, 0);            
	            view.addView(line2view, layoutParams);
            }
//            if (i == listSize - 1) {
//            	TextView line2view = new TextView(mContext);
//	            line2view.setBackgroundResource(R.color.letv_color_ffdfdfdf);
//	            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,1);
//	            view.addView(line2view, layoutParams);
//            }
//            else {
//            	TextView line2view = new TextView(mContext);
//	            line2view.setBackgroundResource(R.color.letv_color_ffdfdfdf);
//	            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,1); 
//	            layoutParams.setMargins(64, 0, 0, 0);            
//	            view.addView(line2view, layoutParams);
//            }
        }
    }
    
    public void refreshWatchFocusView(){
    	if(watchFocusView != null){
    		try {
    			int count = watchFocusView.getChildCount();
    			for (int i = 0; i < count; i++) {
    				View watchingTextView = watchFocusView.getChildAt(i);
    				LogInfo.log("clf", "refreshWatchFocusView...watchingTextView="+watchingTextView);
    				if(watchingTextView instanceof TextView){
    					LogInfo.log("clf", "refreshWatchFocusView1...watchingTextView="+watchingTextView);
    					((TextView)watchingTextView).setTextColor(mContext.getResources().getColor(R.color.letv_color_ff5d5d5d));
    				}
    			}
			} catch (Exception e) {
			}
    	}
    }
    
    /**
     * block图片地址获取
     * @param mVideo
     * @return
     */
    private String getImageUrl(VideoBean mVideo) {
    	String strRetUrl = "";
    	if (mVideo != null) {
    		if (!TextUtils.isEmpty(mVideo.getPic320())) {
    			strRetUrl = mVideo.getPic320();
    		}
    		else if (!TextUtils.isEmpty(mVideo.getPic300())) {
    			strRetUrl =  mVideo.getPic300();
    		}
    		else if (!TextUtils.isEmpty(mVideo.getPic())) {
    			strRetUrl =  mVideo.getPic();
    		}    		
    	}
    	return strRetUrl;
    }


    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
                             ViewGroup parent) {
        VideoBean mVideo = (VideoBean) getChild(groupPosition, childPosition);
        if(mVideo == null ){
            return null;
        }
        //全屏
        if (isLandscape) {
            ViewChildLandscapeHoler landscapeHolder;
            if (convertView == null || !(convertView.getTag() instanceof ViewChildLandscapeHoler)) {
                landscapeHolder = new ViewChildLandscapeHoler();
                convertView = layoutInflater.inflate(R.layout.letv_detailplay_full_expand_list_item_playerlibs, null);
                landscapeHolder.titleView = (TextView)convertView.findViewById(R.id.video_title);
                landscapeHolder.checkView=(ImageView)convertView.findViewById(R.id.player_movie_item_check);
                landscapeHolder.localView = (TextView)convertView.findViewById(R.id.local_tip);
                landscapeHolder.video_title_singer = (TextView)convertView.findViewById(R.id.video_title_singer);
                landscapeHolder.linearlayout_root = convertView.findViewById(R.id.linearlayout_root);
                convertView.getId();
                convertView.setTag(landscapeHolder);
            } else {
                landscapeHolder = (ViewChildLandscapeHoler) convertView.getTag();
            }
            createLandscapeVideosList(landscapeHolder, mVideo, convertView);
        } else {
            ViewChildPortraitHoler mViewHolder = null;
            if (convertView == null || !(convertView.getTag() instanceof ViewChildPortraitHoler)) {
                mViewHolder = new ViewChildPortraitHoler();
                convertView = layoutInflater.inflate(R.layout.letv_detailplay_half_expand_list_item_playerlibs, parent, false);
                mViewHolder.localTextView = (TextView) convertView.findViewById(R.id.videos_list_item_download_state_txt);
                mViewHolder.titleView = (TextView) convertView.findViewById(R.id.videos_list_item_title);
                mViewHolder.subTitleView = (TextView) convertView.findViewById(R.id.videos_list_item_subtitle);
                mViewHolder.playNumView = (TextView) convertView.findViewById(R.id.videos_list_item_playnum);
                mViewHolder.videoTime = (TextView) convertView.findViewById(R.id.video_time);
                mViewHolder.videoImageView = (LetvImageView) convertView.findViewById(R.id.detail_half_video_image);
                mViewHolder.watchFocusView = (LinearLayout) convertView.findViewById(R.id.linearlayout_watch_focus);
                convertView.setTag(mViewHolder);
            } else {
                mViewHolder = (ViewChildPortraitHoler) convertView.getTag();
                clearViewData(mViewHolder);
            }
            createPortraitVideosList(mViewHolder, mVideo, convertView, groupPosition, childPosition);
        }
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if(groupItems!=null && groupPosition < groupItems.size() && groupItems.get(groupPosition) != null) {
        	return groupItems.get(groupPosition).size();
        }
        return 0;
    }

    public int getChildrenItemCount(int groupPosition) {
        if (groupItems != null && groupPosition < groupItems.size() && groupItems.get(groupPosition)!=null) {
            return groupItems.get(groupPosition).size();
        }
        return 0;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupPosition;
    }

    @Override
    public int getGroupCount() {
        return groupCount;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    	if (groupTitles == null || groupPosition >= groupTitles.size()) {
    		return convertView;
    	}
        if (convertView == null || !(convertView.getTag() instanceof ViewHolderGroup)) {
            viewHolderGroup = new ViewHolderGroup();
            if (isLandscape) {
                convertView = layoutInflater.inflate(R.layout.letv_detail_full_expand_parent_selecter_playerlibs, parent, false);
                viewHolderGroup.titleView = (TextView) convertView.findViewById(R.id.title_text);
                viewHolderGroup.arrowView = (ImageView) convertView.findViewById(R.id.arrow);
                viewHolderGroup.progressBarView = (ProgressBar) convertView.findViewById(R.id.play_epsiode_progress);
            }
            else {
                convertView = layoutInflater.inflate(R.layout.letv_detail_half_expand_parent_selecter_playerlibs, parent, false);
                viewHolderGroup.titleView = (TextView) convertView.findViewById(R.id.title_text);
                viewHolderGroup.arrowView = (ImageView) convertView.findViewById(R.id.arrow);
                viewHolderGroup.progressBarView = (ProgressBar) convertView.findViewById(R.id.play_epsiode_progress);
            }
            convertView.setTag(viewHolderGroup);
        } else {
            viewHolderGroup = (ViewHolderGroup) convertView.getTag();
        }
        viewHolderGroup.titleView.setText(groupTitles.get(groupPosition));
        if (isExpanded && getChildrenItemCount(groupPosition) == 0 && loadGroupPosition == groupPosition && loadProgressingFlag) {
            viewHolderGroup.progressBarView.setVisibility(View.VISIBLE);
        } else {
            viewHolderGroup.progressBarView.setVisibility(View.GONE);
        }
        if (!isExpanded) {
            viewHolderGroup.arrowView.setBackgroundResource(R.drawable.letv_arrow_down_new);
            LogInfo.log("hong6", "setBackgroundResource(R.drawable.letv_arrow_down_new)");
        } else {
            viewHolderGroup.arrowView.setBackgroundResource(R.drawable.letv_arrow_up_new);
            LogInfo.log("hong6", "setBackgroundResource(R.drawable.letv_arrow_up_new)");
        }
        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    /**
     * 竖屏状态下的Viewholder
     */
    public static class ViewChildPortraitHoler {
        public LetvImageView videoImageView;
        public TextView titleView;
        public TextView subTitleView;
        public TextView playNumView;
        public TextView localTextView;
        public TextView videoTime;
        public LinearLayout watchFocusView;
    }

    /**
     * 全屏状态下的Viewholder
     */
    public static class ViewChildLandscapeHoler {
        public TextView titleView;
        public TextView localView;
        public ImageView checkView;
        public TextView video_title_singer;
        public View linearlayout_root;
    }


    /**
     * 清空上次使用时的图片资源和监听资源
     * */
    public void clearViewData(ViewChildPortraitHoler mViewHolder) {
        if (mViewHolder.videoImageView != null) {
//            mViewHolder.videoImageView.setImageDrawable(null);
        }
        mViewHolder.localTextView.setText("");
        mViewHolder.titleView.setText("");
        mViewHolder.subTitleView.setText("");
        mViewHolder.playNumView.setText("");
        mViewHolder.videoTime.setText("");
    }

    public void configurePinnedHeader(View v, int position, int alpha) {
        TextView header = (TextView) v;
        header.setText(groupTitles.get(position));
        if (alpha == 255) {
            header.setBackgroundColor(mPinnedHeaderBackgroundColor);
            header.setTextColor(mPinnedHeaderTextColor);
            header.setVisibility(View.VISIBLE);
        } else {
            header.setVisibility(View.GONE);
            header.setBackgroundColor(Color.argb(alpha,
                    Color.red(mPinnedHeaderBackgroundColor),
                    Color.green(mPinnedHeaderBackgroundColor),
                    Color.blue(mPinnedHeaderBackgroundColor)));
            header.setTextColor(Color.argb(alpha,
                    Color.red(mPinnedHeaderTextColor),
                    Color.green(mPinnedHeaderTextColor),
                    Color.blue(mPinnedHeaderTextColor)));
        }
    }

    public int parseData(){
        if(albumNewListByDatePlayerLibs == null){
            return groupCount;
        }

        LinkedHashMap<String,AlbumNewListByDateBean.MonthList> hashMap = albumNewListByDatePlayerLibs.getYearList();
        if (hashMap == null) {
        	return groupCount;
        }
        groupItems.clear();
        Iterator<String> yearKey = hashMap.keySet().iterator();
        groupCount = 0;
        while (yearKey.hasNext()){
            String yearKeyStr = yearKey.next();
            AlbumNewListByDateBean.MonthList monthList = hashMap.get(yearKeyStr);
            if (monthList != null) {
                Iterator<String> monthKey = monthList.keySet().iterator();
                while (monthKey.hasNext()){
                    String monthKeyStr = monthKey.next();
                    groupTitles.add(yearKeyStr+"年"+monthKeyStr+"月");
                    ArrayList<VideoBean> list = new ArrayList<VideoBean>();
                    if (list != null) {
                        list.addAll(monthList.get(monthKeyStr));
                        groupItems.add(list);
                    }
                }
                groupCount +=monthList.size();
            }
        }
        return groupCount;
    }

    public void addDataToGroup(int groupPosition){
    	if (groupTitles.size() > 0 && groupPosition >= 0 && albumNewListByDatePlayerLibs != null) {
	        String groupTitle = groupTitles.get(groupPosition);
	        String yearKey = groupTitle.split("年")[0];
	        String monthKey = groupTitle.split("年")[1].split("月")[0];
	        LinkedHashMap<String,AlbumNewListByDateBean.MonthList> monthList = albumNewListByDatePlayerLibs.getYearList();
	        if (monthList != null && monthList.get(yearKey) != null) {
		        AlbumNewListByDateBean.MonthList month = monthList.get(yearKey);
		        if (month != null) {
    		        ArrayList<VideoBean> videoList = month.get(monthKey);
    		        if (groupItems.get(groupPosition) != null) {
        		        groupItems.get(groupPosition).clear();
        		        if (videoList != null) {
        		        	groupItems.get(groupPosition).addAll(videoList);
        		        }
    		        }
		        }
	        }
    	}
    }

    public ArrayList<ArrayList<VideoBean>> getGroupData(){
        return groupItems;
    }

    public LinkedList<String> getGroupTitle(){
        return groupTitles;
    }
    
    public ArrayList<ArrayList<VideoBean>> getGroupItems(){
        return groupItems;
    }

    public void  setCurId(long curId){
        this.curId = curId;
    }

	public void setData(AlbumNewListByDateBean datePlayerLibs) {
		albumNewListByDatePlayerLibs = datePlayerLibs;
		parseData();
	}

	public void onDestroy(){
		if(watchFocusView != null){
			this.watchFocusView.removeAllViews();
		}
		this.watchFocusView = null;
	}
	
}
